<?php

namespace App\Http\Controllers\Api\Main;

use App\Http\Controllers\ApiController;
use App\Http\Resources\User;
use App\Models\Banner;
use App\Models\Category;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;


class MainController extends ApiController
{

    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 首页轮播图
     * @return array
     */
    public function banner()
    {
        $lists = Banner::orderBy('sort','desc')->get();
        return User::collection($lists);
    }

    /**
     * 获取顶级分类
     * @return
     */
    public function getTopCate()
    {
        $lists = Category::where('pid',0)->get();

        if($lists->count()>0){
            return self::response($lists,self::SUC_CODE);
        }
    }

    /**
     * 获取全部顶级分类对应的二级分类
     */
    public function getCates()
    {
        $lists = DB::table('goods_cate as g')
            ->join('goods_cate as c','g.id','=','c.pid')
            ->where('g.pid',0)
            ->select('g.id as gid','g.pid as gpid','g.cate_name as gcate_name','g.cate_img as gcate_img','c.id as cid','c.pid as cpid','c.cate_name as ccate_name','c.cate_img as ccate_img')
            ->get();
        $cates = array();
        foreach ($lists as $k=>$v){
            $temp =array();
            $temp['id'] = $v->cid;
            $temp['pid'] = $v->cpid;
            $temp['cate_name'] = $v->ccate_name;
            $temp['cate_img'] = $v->ccate_img;
            $cates[$v->gid]['id'] = $v->gid;
            $cates[$v->gid]['pid'] = $v->gpid;
            $cates[$v->gid]['cate_name'] = $v->gcate_name;
            $cates[$v->gid]['cate_img'] = $v->gcate_img;
            $cates[$v->gid]['item'][] = $temp;
        }
        return self::response($cates,self::SUC_CODE);
    }

    /**
     * 获取首页热销商品
     */
    public function getHotGoods()
    {
        $lists = DB::table('order_goods')->select(DB::raw('count(goods_number) as num,sku_id as product_id,goods_id'))->groupBy('sku_id')->orderBy('num','desc')->get();
        //如果热销商品不足4件则使用排序前四的商品
        if($lists->count()<4){
            $lists = DB::table('products')->select('product_id','goods_id')->orderBy('p_order','desc')->get();
        }
        $product_arr = [];
        $count = 0;
        foreach ($lists as $key => $val){
            if($count<4){
                $product_arr[] = $val->product_id;
            }
            $count++;
        }
        $goods = DB::table('products')->join('goods','products.goods_id','=','goods.goods_id')->select('products.*','goods.image')->whereIn('products.product_id',$product_arr)->get();
        return self::response($goods,self::SUC_CODE);

    }

    /**
     * 获取产品列表
     */
    public function getGoods()
    {
        $lists = DB::table('goods')->orderBy('sort','desc')->get();
        return self::response($lists,self::SUC_CODE);
    }

    /**
     * 搜索产品
     * @param title 搜索标题
     */
    public function serachGoods(Request $request)
    {
        $title = htmlspecialchars($request->title);
        $lists = DB::table('goods')->orderBy('sort','desc')->where('goods_name','like',"%$title%")->get();
        if($lists->count()>0){
            return self::response($lists,self::SUC_CODE);
        }else{
            return self::response([],self::SUC_CODE,"未搜索到{$title}相关商品");
        }

    }
}
